/*
 * @Description: enter your description
 * @Autor: bxs
 * @Date: 2021-12-17 13:38:56
 * @LastEditors: bxs
 * @LastEditTime: 2021-12-18 11:01:33
 */
import { currying, getClass, getArgs } from "../基础工具类/basicTools.js"
/**
 * 得到拷贝的值
 * @param {Object} obj1 
 * @param {Object} obj2 
 * @returns 返回拷贝的值
 */
export const copyObj = (obj1, obj2) => {
	return {...obj1,
		...deepCopy(obj2)
	}
}
/**
 * 深拷贝
 * @param {Object} obj 
 * @returns 
 */
 export const deepCopy = (obj) => {
    var result, oClass = getClass(obj);
    if (oClass == "Object") {
		result = {}
	} //判断传入的如果是对象，继续遍历
    else if (oClass == "Array") {
			result = []
		} //判断传入的如果是数组，继续遍历
		else {
			return obj
		}//如果是基本数据类型就直接返回
    for (var i in obj) {
        var copy = obj[i];
        if (getClass(copy) == "Object") {
			result[i] = deepCopy(copy)
		} //递归方法 ，如果对象继续变量obj[i],下一级还是对象，就obj[i][i]
        else if (getClass(copy) == "Array") { 
				result[i] = deepCopy(copy)
			}//递归方法 ，如果对象继续数组obj[i],下一级还是数组，就obj[i][i]
			else{ 
				result[i] = copy
			} //基本数据类型则赋值给属性
    }
    return result;
}
/**
 * 调用Hook函数
 * @param {Object} obj 调用对象
 * @param {String} Hooks 钩子
 * @param  {...any} rest  剩余参数
 */
export const callHooks = (obj, Hooks, ...rest) => {
	if (obj[Hooks] !== undefined
		&& getClass(obj[Hooks]) === 'Function') {
			obj[Hooks].apply(obj, rest)
	}
}
/**
 * Hooks函数变量
 */
const HOOK = {
	beforeGetName: 'beforeGetName',
	afterGetName: 'afterGetName',
	beforeGetForm: 'beforeGetForm',
	afterGetForm: 'afterGetForm'
}

/**
 *下面是例子
 * 可以通过调用
 * obj.getName()
 * 来观察情况
 */
let obj = {
	name: 'name',
	form: {
		userName: 'admin',
		passWord: '000000'
	},
	HOOK,
	getName() {
		// 自定义hooks一般要指定hooks的参数
		callHooks(obj, obj.HOOK.beforeGetName, this.name)
		console.log('执行getName')
	},
	getForm() {
		callHooks(obj, obj.HOOK.beforeGetForm, this.form)
		console.log(`执行getForm :`,this.form)
	}
}
obj[obj.HOOK.beforeGetName] = (name) => {
	console.log(`beforeName is :${name}`)
}
obj[obj.HOOK.beforeGetForm] = (form) => {
	form.userName = 'superAdmin'
}
obj.getForm()